<?php

class Database 
{  
     static private $host = 'localhost',
              $user = 'shared',
              $pw   = 'test1337', 
              $name = 'azubi',
              $dbh  = null;
      
      //construct connection to db 'guestbook'
      // __ magic function instant connect to db
      static public function getConnection() {
          if (!self::$dbh) {
            $con = 'mysql:dbname=' . self::$name . ';host=' . self::$host; 
            try { 
                //db::$dbh for access class db(class variables)
                self::$dbh = new PDO ($con, self::$user, self::$pw, array 
                    ( 
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
                    ) 
                ); 
            } catch(PDOException $e) { 
               echo 'error opening the db.<br><br>'; 
               echo $e->getMessage(); exit(); 
            }
          }
          return self::$dbh;
      }
      
}
?>
